package com.sustcoder.blog.Blog09;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sustcoder.blog.Blog09.model.Bean1;
import com.sustcoder.blog.Blog09.model.Bean2;
import com.sustcoder.blog.Blog09.model.ChildJsonBean;
import com.sustcoder.blog.Blog09.model.JsonBean;

/**
 * Created by lenovo on 2017/9/29.
 */
public class Json2Bean {
    public static void main(String[] args) throws Exception{
        // sameProName();// bean和json字段名称一致
        // diffName();// bean和json字段名称不一致
        haveObj();
    }

    public static void haveObj() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        JSONObject child=new JSONObject();
        child.put("id",1);
        child.put("name","damo");
        JSONObject json=new JSONObject();
        json.put("meassage","pro");
        json.put("bean",child);
        Bean2 bean2= mapper.readValue(json.toString(), Bean2.class);
        System.out.println("child format rst: "+bean2.getBean1().getName());
    }

    public static void  sameProName() throws  Exception{
        JSONObject jsonStr=new JSONObject();
        jsonStr.put("id",1);
        jsonStr.put("name","damo");
        Bean1 bean1 = JSON.parseObject(jsonStr.toJSONString(), Bean1.class); // json键和bean键一致时使用
        System.out.println(bean1.getId());
    }
    public static void diffName() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr="{\"filename\":\"cardata/2016/10/31/949626/4F100EF6-BF9D-4C48-825F-CA6FF69A72F6.png\",\"size\":\"172915\",\"mimeType\":\"image/png\",\"etag\":\"491CBFBA26700057811D5A38D5549316\",\"height\":\"509\",\"width\":\"677\",\"outno\":\"949626\",\"origfilename\":\"fortest.png\",\"filetype\":\"征信查询授权书\",\"filefulltype\":\"/主申请人资料/征信查询授权书\",\"tplverno\":\"1000000012\",\"crtsource\":\"cmis\",\"crtuser\":\"huangyan\",\"osparentno\":\"100000001142\",\"userid\":\"null\"}";
        ChildJsonBean fileInfo= mapper.readValue(jsonStr, ChildJsonBean.class);
        System.out.println("child format rst: "+fileInfo.getOsOutNo());
        JsonBean jsonBean=(JsonBean)fileInfo;
        System.out.println("parent format rst:"+jsonBean.getOsFileName());
        String verifyRst=fileInfo.verfiyParams();
        System.out.println("verifyRst rst: "+verifyRst);
    }
}
